//
//  MerchantCardVC.m
//  XY
//
//  Created by 洛水寒 on 16/5/11.
//  Copyright © 2016年 洛水寒. All rights reserved.
//

#import "MerchantCardVC.h"
#import "DownView.h"
#import "MyAlertView.h"
#import "MerchantDgVC.h"
@interface MerchantCardVC ()<UITextFieldDelegate>
{
    int picture;
    NSMutableArray *imgArray;
}
@property (weak, nonatomic) IBOutlet UITextField *bankTF;
@property (weak, nonatomic) IBOutlet UITextField *bankCardTF;
@property (weak, nonatomic) IBOutlet UITextField *bankSiteTF;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UIButton *btn4;
@property (weak, nonatomic) IBOutlet UIButton *btn5;

@property (weak, nonatomic) IBOutlet UIButton *btn6;

@end

@implementation MerchantCardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"商户申请";
    self.edgesForExtendedLayout = UIRectEdgeNone;//跳过64
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];//去掉返回的字
    self.navigationItem.backBarButtonItem = item;
    imgArray = [NSMutableArray arrayWithObjects:@"3",@"3",@"3",@"3",@"3",@"3", nil];
    _bankTF.enabled = NO;
    _bankTF.delegate = self;
    _bankCardTF.delegate = self;
    _bankSiteTF.delegate = self;
}
//选择开户行
- (IBAction)chooseBank:(id)sender
{    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"工商",@"邮政",@"建设",@"呵呵",@"我擦",@"呵呵", nil];
    DownView *view = [[DownView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-110, 88+64, 110, array.count*30) titleArray:array withTag:2];
    view.clickedBtnIndexBlock = ^(NSInteger index)
    {
        
        _bankTF.text = [array objectAtIndex:index];
        
    };
    
    [view show];

}
//下一步
- (IBAction)nextBtn:(id)sender
{
    //containsObject 判断
    if ([imgArray containsObject:@"3"]) {
        //证件照必须是6张
    }
    
    MerchantDgVC *merchantDgVC = [[MerchantDgVC alloc]init];
    [self.navigationController pushViewController:merchantDgVC animated:YES];
}

//上传证件照
- (IBAction)addImgBtnClick:(UIButton *)btn
{
    
    picture = btn.tag;
    
    MyAlertView *alertView = [[MyAlertView alloc]init];
    
    alertView.lab.text = @"上传头像";
    
    [alertView.btn setTitle:@"拍照" forState:UIControlStateNormal];
    [alertView.btn setImage:[UIImage imageNamed:@"拍照"] forState:UIControlStateNormal];
    alertView.btn2.titleLabel.font = [UIFont systemFontOfSize:14];
    alertView.btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [alertView.btn2 setTitle:@"相册" forState:UIControlStateNormal];
    [alertView.btn2 setImage:[UIImage imageNamed:@"相册"] forState:UIControlStateNormal];
    
    alertView.clickedBtnIndexBlock = ^(NSInteger index)
    {
       
        if (index == 0)
        {
            [self takePhoto];
        }
        else{
            
            [self localPhoto];
        }
    };
    
    [alertView show];
    

}
///---

//拍照
-(void)takePhoto
{
    //资源类型为照相机
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    //判断是否有相机
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        //资源类型为照相机
        picker.sourceType = sourceType;
        
        
        [self presentViewController:picker animated:YES completion:nil];
    }else {
        ////DBLOG(@"该设备无摄像头");
    }
}

//从相册选择
-(void)localPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    //资源类型为图片库
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    picker.navigationBar.tintColor = [UIColor blueColor];
    
    [self presentViewController:picker animated:YES completion:nil];
    
}
//图像选取器的委托方法，选完图片后回调该方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    
    
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *data= UIImageJPEGRepresentation(newImage, 0.1);
    NSString *str = [data base64Encoding];
    
    // NSString *result = [[NSString alloc] initWithData:data  encoding:NSJapaneseEUCStringEncoding];
    
    [picker dismissViewControllerAnimated:YES completion:^{
        switch (picture) {
            case 1001:
            {
                [_btn1 setImage:newImage forState:UIControlStateNormal];
                [imgArray replaceObjectAtIndex:0 withObject:str];
            }
                break;
            case 1002:
            {
               [_btn2 setImage:newImage forState:UIControlStateNormal];
                 [imgArray replaceObjectAtIndex:1 withObject:str];
            }
                break;
            case 1003:
            {
                [_btn3 setImage:newImage forState:UIControlStateNormal];
                 [imgArray replaceObjectAtIndex:2 withObject:str];
            }
                break;
            case 1004:
            {
                [_btn4 setImage:newImage forState:UIControlStateNormal];
                 [imgArray replaceObjectAtIndex:3 withObject:str];
            }
                break;
            case 1005:
            {
                [_btn5 setImage:newImage forState:UIControlStateNormal];
                 [imgArray replaceObjectAtIndex:4 withObject:str];
            }
                break;
            case 1006:
            {
                [_btn6 setImage:newImage forState:UIControlStateNormal];
                 [imgArray replaceObjectAtIndex:5 withObject:str];
            }
                break;
                
            default:
                break;
        }
        
        
    }];

    
}

#pragma mark --去键盘

- (void)_handleEndEditing
{
    [self.view endEditing:YES];
}

//当用户按下return去键盘

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{
    
    
[textField resignFirstResponder];
        
    
    return YES;
    
}

//空白去键盘

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{
    
    [self.view.window endEditing:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
